package com.hundun.hddeal.controller;


import com.aliyuncs.exceptions.ClientException;
import com.hundun.hddeal.dao.AdUserMapper;
import com.hundun.hddeal.dao.MtUserMapper;
import com.hundun.hddeal.entity.AdUser;
import com.hundun.hddeal.entity.MtUser;
import com.hundun.hddeal.response.Result;
import com.hundun.hddeal.response.ResultUtil;
import com.hundun.hddeal.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 忘记密码发送短信及验证 \\ 发送图片验证码
 * @author 谢
 */


@RestController
@RequestMapping("note")
@Slf4j
public class NoteController {

    @Autowired
    AdUserMapper adUserMapper;
    @Autowired
    MtUserMapper mtUserMapper;
    @Autowired
    RedisUtil redisUtil;


    /**
     * 广告方发送短信验证
     * @param map
     * @return
     * @throws ClientException
     */
    @PostMapping("ad")
    public Result sendAdNote(@RequestBody Map<String, String> map) throws ClientException {
        String adPhone=map.get("adPhone");
        if(adPhone!=null){
            String randomNum = NoteUtil.sendNote(adPhone);
            redisUtil.set(adPhone,randomNum,300);
            return ResultUtil.success();
        }
        String adId = map.get("adId");
        if(adId==null){
            return ResultUtil.error("用户名为空!");
        }
        AdUser adUser = adUserMapper.selectByAdUserId(adId);
        if (adUser != null) {
            if (adUser.getAdPhone() == null) {
                return ResultUtil.error("用户注册电话号码为空!");
            }
            String randomNum = NoteUtil.sendNote(adUser.getAdPhone());
            redisUtil.set("adId"+adUser.getAdId(),randomNum,300);
            return ResultUtil.success();
        }
        return ResultUtil.error("不存在用户");
    }

    /**
     * 媒体方发送短信验证
     * @param map
     * @return
     * @throws ClientException
     */
    @PostMapping("mt")
    public Result sendMtNote(@RequestBody Map<String, String> map) throws ClientException {
        String mtId = map.get("mtId");
        MtUser mtUser;
        if(mtId==null){
            mtId=map.get("mtPhone");
            mtUser = mtUserMapper.selectPhone(mtId);
        } else {
            mtUser = mtUserMapper.selectByMtUserId(mtId);
        }
        if (mtUser != null) {
            if (mtUser.getMtPhone() == null) {
                return ResultUtil.error("用户注册电话号码为空!");
            }
            String randomNum = NoteUtil.sendNote(mtUser.getMtPhone());
            redisUtil.set("mtId"+mtUser.getMtId(),randomNum,300);
            return ResultUtil.success();
        }else {
            String randomNum = NoteUtil.sendNote(mtId);
            redisUtil.set(mtId,randomNum,300);
            return ResultUtil.success();
        }

    }


    @PostMapping("login")
    public Result sendAdLogin(@RequestBody Map<String, String> map) throws ClientException {
        if(map.get("adPhone")==null && map.get("mtPhone")==null){
            return ResultUtil.error("手机号为空");
        }
        if(map.get("mtPhone")!=null){
            MtUser mtUser = mtUserMapper.selectPhone(map.get("mtPhone"));
            if(mtUser==null){
                return ResultUtil.error("不存在该用户");
            }
            String randomNum = NoteUtil.getLoginNote(map.get("mtPhone"));
            redisUtil.set(map.get("mtPhone"),randomNum,300);
            return ResultUtil.success();
        }else {
            AdUser adUser= adUserMapper.loginForPhone(map.get("adPhone"));
            if(adUser==null){
                return ResultUtil.error("不存在该用户");
            }
            String randomNum = NoteUtil.getLoginNote(map.get("adPhone"));
            redisUtil.set(map.get("adPhone"),randomNum,300);
            return ResultUtil.success();
        }
    }




    /**
     * 重置密码验证
     * @param map
     * @return
     */
    @PostMapping("verify")
    public Result eqNote(@RequestBody Map<String, String> map) {
        String userCode;
        if(map.get("adId")!=null){
            String adNote =(String)redisUtil.get("adId"+map.get("adId"));
            if(adNote==null){
                return ResultUtil.error("验证码已失效");
            }
            userCode=adNote;
        }else {
            String mtNote =(String)redisUtil.get("mtId"+map.get("mtId"));
            if(mtNote==null){
                return ResultUtil.error("验证码已失效");
            }
            userCode=mtNote;
        }
        String code =(String)redisUtil.get(map.get("code").toUpperCase());
        if(code ==null){
            return ResultUtil.error("图形验证码不正确");
        }
        log.info(map.get("code").toUpperCase());
        log.info(code);
        String getNote = map.get("note");
        String getCode = map.get("code");
        if (userCode.equals(getNote)&&code .equals(getCode.toUpperCase())) {
            if(map.get("adId")!=null){
                redisUtil.del("adId"+map.get("adId"));
            }else {
                redisUtil.del("mtId"+map.get("mtId"));
            }
            redisUtil.del(map.get("code"));
            return ResultUtil.success("验证成功");
        }
        return ResultUtil.error("验证码不正确");
    }


    /**
     * 重置支付密码验证
     * @param map
     * @return
     */
    @PostMapping("payVerify")
    public Result eqPayNote(@RequestBody Map<String, String> map){
        String token=map.get("adUserToken");
        String adUserId = TokenUtilT.getTokenById(token);
        AdUser adUser = adUserMapper.findAdUserById(adUserId);
        String adNote = (String)redisUtil.get(adUser.getAdPhone());
        if(adNote==null){
            return ResultUtil.error("验证码已失效");
        }
        String getNote = map.get("note");
        if(adNote.equals(getNote)){
            redisUtil.del(adUser.getAdPhone());
            adUserMapper.resetAdPwd(null,adUserId,MD5Util.getSaltMD5(map.get("newPayPwd")));
            return ResultUtil.success("修改成功");
        }
        return ResultUtil.error("验证码不正确");
    }







    /**
     * 获取验证码图片
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping("formatName")
    public void formatName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //设置响应头信息，通知浏览器不要缓存
        response.setHeader("Expires", "-1");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "-1");
        response.setContentType("image/jpeg");

        // 获取验证码
        ValidateCodeUtil vCode = new ValidateCodeUtil(160, 40, 5, 150);
        String code = vCode.getCode();
        // 将验证码输入到redis中，用来验证
        redisUtil.set(code,code,300);
        // 输出到web页面
        vCode.write(response.getOutputStream());
    }









}
